using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SpoonCore2.Client;
using SpoonCore2.Message;

namespace SpoonCore2.Service.Broadcast
{
	public class BroadcastService : AbstractService
	{
		private readonly SpoonIdentifier _id = SpoonIdentifier.Parse("16113AC34F86EAA04561F6FA24B46588");
		private readonly SortedList<SpoonIdentifier, BroadcastGroup> _groups = new SortedList<SpoonIdentifier, BroadcastGroup>();

		public override SpoonIdentifier Id { get { return _id; } }

		public BroadcastGroup Join(SpoonIdentifier id)
		{
			return new BroadcastGroup(this, id);
		}

		public BroadcastGroup JoinNew()
		{
			return Join(new SpoonIdentifier());
		}

		public override void Dispose()
		{

		}

		public override void Init()
		{
			Handler.Register(new MessageHandler.ReceiveMessageHandler<BroadcastMessage>(ReceiveBroadcast));
		}

		private void ReceiveBroadcast(ClientInfo sender, BroadcastMessage message)
		{
			if (_groups.ContainsKey(message.BroadcastGroup))
			{
				_groups[message.BroadcastGroup].Receive(message.Data);
			}
		}

		public override void Start()
		{

		}

		public override void Stop()
		{

		}
	}
}